﻿using System;
using System.Drawing;
using System.Web;

namespace sBlog.Net.Upload
{
    public class MlogUploadThumbPost : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Expires = -1;
            if (context.Request["UserId"] != null)
            {
                string userId = context.Request["UserId"];
                string fName = Guid.NewGuid().ToString() + ".jpg";
                try
                {
                    HttpPostedFile postedFile = context.Request.Files["Filedata"];
                    Byte[] buffer = new Byte[1];

                    string ftpUploadUrl = string.Empty;
                    string strFtpAddress = System.Configuration.ConfigurationManager.AppSettings["ftpServer"].ToString() + "/Mlog/UploadThumbs";
                    string strFolderUser = clsFunction.GetFolderUser(strFtpAddress, userId);
                    clsFunction.CreateUserFolder(strFtpAddress, userId, System.Configuration.ConfigurationManager.AppSettings["ftpUserName"].ToString(), System.Configuration.ConfigurationManager.AppSettings["ftpPassword"].ToString());

                    //80x80_
                    Bitmap bmp80 = new Bitmap(postedFile.InputStream);
                    if (bmp80.Width > 80 || bmp80.Height > 80)
                    {
                        Bitmap bmpResize = ImageUtility.cropImageRectangleCenter(bmp80, new Rectangle(0, 0, 80, 80));
                        buffer = ImageUtility.SaveImage(bmpResize, 100L);
                        bmp80.Dispose();
                        bmpResize.Dispose();
                    }
                    else buffer = ImageUtility.SaveImage(bmp80, 100L);
                    ftpUploadUrl = strFolderUser + "/80x80_" + fName;
                    clsFunction.UpFileServer(ftpUploadUrl, buffer);

                    //110x120_
                    Bitmap bmp110 = new Bitmap(postedFile.InputStream);
                    if (bmp110.Width > 110 || bmp110.Height > 120)
                    {
                        Bitmap bmpResize = ImageUtility.cropImageRectangleCenter(bmp110, new Rectangle(0, 0, 110, 120));
                        buffer = ImageUtility.SaveImage(bmpResize, 100L);
                        bmp110.Dispose();
                        bmpResize.Dispose();
                    }
                    else buffer = ImageUtility.SaveImage(bmp110, 100L);
                    ftpUploadUrl = strFolderUser + "/110x120_" + fName;
                    clsFunction.UpFileServer(ftpUploadUrl, buffer);

                    //630x303_
                    Bitmap bmp630 = new Bitmap(postedFile.InputStream);
                    if (bmp630.Width > 630 || bmp630.Height > 303)
                    {
                        Bitmap bmpResize = ImageUtility.cropImageRectangleCenter(bmp630, new Rectangle(0, 0, 630, 303));
                        buffer = ImageUtility.SaveImage(bmpResize, 100L);
                        bmp630.Dispose();
                        bmpResize.Dispose();
                    }
                    else buffer = ImageUtility.SaveImage(bmp630, 100L);
                    ftpUploadUrl = strFolderUser + "/630x303_" + fName;
                    clsFunction.UpFileServer(ftpUploadUrl, buffer);

                    //70x44_
                    Bitmap bmp70 = new Bitmap(postedFile.InputStream);
                    if (bmp70.Width > 70 || bmp70.Height > 44)
                    {
                        Bitmap bmpResize = ImageUtility.cropImageRectangleCenter(bmp70, new Rectangle(0, 0, 70, 44));
                        buffer = ImageUtility.SaveImage(bmpResize, 100L);
                        bmp70.Dispose();
                        bmpResize.Dispose();
                    }
                    else buffer = ImageUtility.SaveImage(bmp70, 100L);
                    ftpUploadUrl = strFolderUser + "/70x44_" + fName;
                    clsFunction.UpFileServer(ftpUploadUrl, buffer);

                    //196x196_
                    Bitmap bmp196 = new Bitmap(postedFile.InputStream);
                    if (bmp196.Width > 196 || bmp196.Height > 196)
                    {
                        Bitmap bmpResize = ImageUtility.cropImageRectangleCenter(bmp196, new Rectangle(0, 0, 300, 200));
                        buffer = ImageUtility.SaveImage(bmpResize, 100L);
                        bmp196.Dispose();
                        bmpResize.Dispose();
                    }
                    else buffer = ImageUtility.SaveImage(bmp196, 100L);
                    ftpUploadUrl = strFolderUser + "/196x196_" + fName;
                    clsFunction.UpFileServer(ftpUploadUrl, buffer);

                    string strDate = DateTime.Now.Year.ToString() + DateTime.Now.ToString("MM");

                    context.Response.StatusCode = 200;
                    context.Response.Write(strDate + fName);
                }
                catch (Exception ex)
                {
                    clsFunction.Log("Loi ThumbPost: " + ex.Message);
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}